package com.unf.edu.rms.controller.facultyresearch;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.unf.edu.rms.controller.userlogin.UserLoginController;
import com.unf.edu.rms.controller.validation.FacultyResearchValidator;
import com.unf.edu.rms.dao.Category;
import com.unf.edu.rms.dao.Faculty;
import com.unf.edu.rms.dao.FacultyResearch;
import com.unf.edu.rms.service.CategoryService;
import com.unf.edu.rms.service.FacultyResearchService;
import com.unf.edu.rms.service.FacultyService;
import com.unf.edu.rms.util.RMSConstants;

@Controller
public class FacultyResearchController {

	@Autowired
	FacultyResearchService facultyResearchService;
	@Autowired
	private FacultyService facultyService;
	@Autowired
	private CategoryService categoryService;
	@Autowired
	private UserLoginController userLoginController;

	@RequestMapping("/viewFacultyResearchList")
	public String viewFacultyResearchList(Model model) {
		System.out.println("FacultyController.viewFacultyList()==========");
		List<FacultyResearch> facultyResearchList = facultyResearchService.getFacultyResearchList();
		
		model.addAttribute("facultyResearchList", facultyResearchList);
		return RMSConstants.FACULTY_RESEARCH_LIST_VIEW;
	}
	
	@RequestMapping("/printFacultyResearchList")
	public String printFacultyResearchList(Model model) {
		System.out.println("FacultyController.printFacultyList()==========");
		List<FacultyResearch> facultyResearchList = facultyResearchService.getFacultyResearchList();
		
		model.addAttribute("facultyResearchList", facultyResearchList);
		return RMSConstants.FACULTY_RESEARCH_LIST_PRINT;
	}

	@RequestMapping(value = "/viewNewFacultyResearch", method = RequestMethod.POST)
	public String viewNewFacultyResearch(@ModelAttribute("facultyResearch") FacultyResearch facultyResearch,Model model) {
		if(userLoginController.checkValidateSession()==0){
			return "redirect:Dashboard.html";
		}
		List<Faculty> facultyList = facultyService.getFacultyList();
		Map<Integer,String> facultyMap = new HashMap<Integer, String>();
		for(Faculty faculty:facultyList)
		{
			facultyMap.put(faculty.getFacultyId(),faculty.getFirstName()+" "+faculty.getLastName());
		}
		model.addAttribute("facultyList", facultyMap);
		
		List<Category> categoryList = categoryService.viewCategoryTable();
		Map<Integer,String> categoryMap = new HashMap<Integer, String>();
		for(Category category:categoryList){
			categoryMap.put(category.getCategoryId(), category.getCategoryName());
		}
		model.addAttribute("categoryList",categoryMap);
		
		return RMSConstants.ADD_FACULTY_RESEARCH_VIEW;
	}

	@RequestMapping(value = "/addNewFacultyResearch", method = RequestMethod.POST)
	public String addNewFacultyResearch(@ModelAttribute("facultyResearch") FacultyResearch facultyResearch,BindingResult bindingResult,Model model) {

		FacultyResearchValidator facultyresearchValidator = new FacultyResearchValidator();
		facultyresearchValidator.validate(facultyResearch, bindingResult);
		
		if(!bindingResult.hasErrors()){
			facultyResearchService.addNewFacultyResearch(facultyResearch);
			List<FacultyResearch> facultyResearchList = facultyResearchService
					.getFacultyResearchList();
			model.addAttribute("facultyResearchList", facultyResearchList);
			return RMSConstants.FACULTY_RESEARCH_LIST_VIEW;
		}
		else{
			List<Faculty> facultyList = facultyService.getFacultyList();
			Map<Integer,String> facultyMap = new HashMap<Integer, String>();
			for(Faculty faculty:facultyList)
			{
				facultyMap.put(faculty.getFacultyId(),faculty.getFirstName()+" "+faculty.getLastName());
			}
			model.addAttribute("facultyList", facultyMap);
			
			List<Category> categoryList = categoryService.viewCategoryTable();
			Map<Integer,String> categoryMap = new HashMap<Integer, String>();
			for(Category category:categoryList){
				categoryMap.put(category.getCategoryId(), category.getCategoryName());
			}
			model.addAttribute("categoryList",categoryMap);
			return RMSConstants.ADD_FACULTY_RESEARCH_VIEW;
		}
	}

	@RequestMapping(value = "/viewExistingFacultyResearch", method = RequestMethod.POST)
	public String viewExitingFacultyResearch(@RequestParam(required=true) int facultyResearchId,Model model) {
		
		if(userLoginController.checkValidateSession()==0){
			return "redirect:Dashboard.html";
		}
		
		FacultyResearch facultyresearch = facultyResearchService.getFacultyResearchById(facultyResearchId);
		model.addAttribute("facultyResearch",facultyresearch);
		
		List<Faculty> facultyList = facultyService.getFacultyList();
		Map<Integer,String> facultyMap = new HashMap<Integer, String>();
		for(Faculty faculty:facultyList)
		{
			facultyMap.put(faculty.getFacultyId(),faculty.getFirstName()+" "+faculty.getLastName());
		}
		model.addAttribute("facultyList", facultyMap);
		
		List<Category> categoryList = categoryService.viewCategoryTable();
		Map<Integer,String> categoryMap = new HashMap<Integer, String>();
		for(Category category:categoryList){
			categoryMap.put(category.getCategoryId(), category.getCategoryName());
		}
		model.addAttribute("categoryList",categoryMap);
		
		return RMSConstants.ADD_FACULTY_RESEARCH_VIEW;
	}
	
	@RequestMapping(value = "/updateFacultyResearch", method = RequestMethod.POST)
	public String updateFacultyResearch(@RequestParam(required=true) int facultyResearchId,@ModelAttribute("facultyResearch") FacultyResearch facultyResearch,BindingResult bindingResult,Model model) {
		
		FacultyResearchValidator facultyresearchValidator = new FacultyResearchValidator();
		facultyresearchValidator.validate(facultyResearch, bindingResult);
		
		if(!bindingResult.hasErrors()){
		
			facultyResearch.setFacultyResearchId(facultyResearchId);
			facultyResearchService.updateFacultyResearch(facultyResearch);
			return "redirect:viewFacultyResearchList.html";
		}
		else{
			List<Faculty> facultyList = facultyService.getFacultyList();
			Map<Integer,String> facultyMap = new HashMap<Integer, String>();
			for(Faculty faculty:facultyList)
			{
				facultyMap.put(faculty.getFacultyId(),faculty.getFirstName()+" "+faculty.getLastName());
			}
			model.addAttribute("facultyList", facultyMap);
			
			List<Category> categoryList = categoryService.viewCategoryTable();
			Map<Integer,String> categoryMap = new HashMap<Integer, String>();
			for(Category category:categoryList){
				categoryMap.put(category.getCategoryId(), category.getCategoryName());
			}
			model.addAttribute("categoryList",categoryMap);
			return RMSConstants.ADD_FACULTY_RESEARCH_VIEW;
		}
	}
	
	@RequestMapping(value = "/viewFacultyResearchDetails", method = RequestMethod.POST)
    public String viewFacultyResearchDetails(@RequestParam(required=true) int facultyResearchId, Model model){
        FacultyResearch facultyResearch = facultyResearchService.getFacultyResearchById(facultyResearchId);
        model.addAttribute("facultyResearch", facultyResearch);
        return RMSConstants.FACULTY_RESEARCH_DETAILS_VIEW;
    }
	
	@RequestMapping(value = "/printFacultyResearchDetails", method = RequestMethod.POST)
    public String printFacultyResearchDetails(@RequestParam(required=true) int facultyResearchId, Model model){
        FacultyResearch facultyResearch = facultyResearchService.getFacultyResearchById(facultyResearchId);
        model.addAttribute("facultyResearch", facultyResearch);
        return RMSConstants.FACULTY_RESEARCH_DETAILS_PRINT;
    }
}